<?php
/*
 * Copyright (c) 2013 Eugene Kuzmenko
 *
 * Permission  is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in  the Software without restriction, including without limitation the rights
 * to  use,  copy,  modify,  merge, publish, distribute, sublicense, and/or sell
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above  copyright  notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * For more information, see <http://www.aljey.com>.
 */

namespace Al\Style\Lib\Form;

use Al\Core\Lib\ConfigurableInterface;
use Al\Core\Lib\ParameterHolder,
    Al\Core\Lib\Service;

/**
 * The form field - a container of element(s).
 *
 * @property string $label A field label (automatically localized).
 * @property int $labelWidth A field label width.
 * @property array $items Configs of the field child items.
 * @property string $layout Form layout: basic, horizontal or inline.
 * @property string $id The field id.
 * @author Eugene Kuzmenko <mail@aljey.com>
 */
class Field extends ParameterHolder implements ConfigurableInterface {

  /**
   * @var Form A form instance.
   */
  public $form;

  /**
   * @var bool True if at least one of the field items is required.
   */
  protected $required = false;

  /**
   * @var array A configuration array.
   */
  protected $config = array(
    'label' => '',
    'labelWidth' => 2,
    'items' => array(),
  );

  /**
   * @param array $params
   * @param Form $form
   */
  public function __construct($params = array(), Form $form = null) {
    $this->form = $form ? $form : new Form;
    $this->layout = $this->form->getLayout();
    $this->id = uniqid('field-');
    Service::mergeRecursive($this->config, $params);
    $data = array();
    foreach ($this->items as $key => $value) {
      $data[$key] = $this->createItem($key, $value);
      if ($data[$key]->required) $this->required = true;
    }
    parent::__construct($data);
  }

  /**
   * Creates a field item.
   *
   * @param string $name The form field name, as submitted to the server.
   * @param array $params A configuration array.
   * @return \Al\Style\Lib\Form\Element\Element
   */
  private function createItem($name, $params = array()) {
    $config = array('type' => 'text', 'items' => array());
    Service::mergeRecursive($config, $this->config);
    Service::mergeRecursive($config, $params);
    $config['name'] = $name;
    if (!($type = Service::get('config.plugins.Al\Style.element.' .
        $config['type']))) {
      throw new \RuntimeException("Unknown form element {$config['type']}");
    }
    return new $type($config, $this);
  }

  /**
   * Returns a string representation of the field.
   *
   * @return string
   */
  public function __toString() {
    $result = Service::get('template')->render(
      Service::get('config.plugins.Al\Style.form.field'), array(
        'items' => $this,
        'layout' => $this->layout,
        'label' => Service::get('config')->trans($this->label),
        'required' => $this->required,
        'id' => $this->id,
        'labelWidth' => $this->labelWidth,
      ));
    return $result[0];
  }

  /**
   * Returns a configuration value as if it was a property of the class.
   *
   * @param string $name
   * @return mixed
   */
  public function __get($name) {
    return isset($this->config[$name]) ? $this->config[$name] : null;
  }

  /**
   * Sets a configuration value as if it was a property of the class.
   *
   * @param string $name
   * @param mixed $value
   */
  public function __set($name, $value) {
    $this->config[$name] = $value;
  }

} 